package com.house365.core.util.lbs;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.provider.Settings;

import com.google.android.maps.GeoPoint;
import com.house365.core.R;
import com.house365.core.inter.ConfirmDialogListener;
import com.house365.core.util.AbstractCallBack;
import com.house365.core.util.ActivityUtil;

/**
 * @author py
 * @date 2012-7-27
 * @comment
 */
public class MapUtil {
	private static final double RADIUS = 6.371229 * 1e6;
	private static final double PI = 3.14159265;

	private static final double ItudePer = 0.00899289;
	public static int latlongTometre = 110950;

	private final static int DEVIANTLAT = -2083; // 偏移维度
	private final static int DEVIANTLNG = 5169; // 偏移经度

	public static double distance(double wd1, double jd1, double wd2, double jd2) {
		double x, y, out;
		x = (jd2 - jd1) * PI * RADIUS * Math.cos(((wd1 + wd2) / 2) * PI / 180)
				/ 180;
		y = (wd2 - wd1) * PI * RADIUS / 180;
		out = Math.hypot(x, y);
		return out / 1000;
	}

	public static double getLatPer() {
		return ItudePer;
	}

	public static double getLngPer(double lat) {
		return ItudePer * Math.cos(lat);
	}

	public static String getDistance(double p1Lat, double p1Lng, double p2Lat,
			double p2Lng) {
		double distance = distance(p1Lat, p1Lng, p2Lat, p2Lng) * 1000; // �������
		if (distance > 10000) {
			return (int) (distance / 1000) + "km";
		} else {
			return (int) distance + "m";
		}
	}
	
	public static String getDistance(Location loaction, double p2Lat,
			double p2Lng) {
		
		return getDistance(loaction.getLatitude(),
				loaction.getLongitude(), p2Lat, p2Lng);
	}
	
	public static String getDistanceDecimal(double p1Lat, double p1Lng, double p2Lat,
			double p2Lng) {
		double distance = distance(p1Lat, p1Lng, p2Lat, p2Lng) * 1000; // �������
		if (distance > 1000) {
			double quotient1 = distance / 1000;
			int quotient2 = ((int)distance / 1000);
			if(((int)distance % 1000)==0){
				return quotient2 + "km";
			}else {
				java.math.BigDecimal b = new java.math.BigDecimal(quotient1);  
				double value = b.setScale(2, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();  
				return value + "km";
			}
		} else {
			return (int)distance + "m";
		}
	}
	
	public static String getDistanceDecimal(Location loaction, double p2Lat,
			double p2Lng) {
		
		return getDistanceDecimal(loaction.getLatitude(),
				loaction.getLongitude(), p2Lat, p2Lng);
	}

	public static double[][] GetTwoPointLocation(double p1Lat, double p1Lng,
			int distance) {
		// double secDistance =
		// Math.floor(Math.cos(45)*distance/latlongTometre)/converUnit;
		// double triangle = Math.cos(45)*distance;
		double triangle = Math.sqrt(Math.pow(distance, 2) / 2);
		// double secDistance = Math.floor(triangle/latlongTometre);
		double secDistance = triangle / latlongTometre;
		double twoPointLoact[][] = new double[2][2];
		twoPointLoact[0][0] = p1Lat + secDistance;
		twoPointLoact[0][1] = p1Lng - secDistance;
		twoPointLoact[1][0] = p1Lat - secDistance;
		twoPointLoact[1][1] = p1Lng + secDistance;
		return twoPointLoact;
	}

	public static GeoPoint getPoint(Location location) {
		GeoPoint geoPoint = null;
		if (location != null) {
			double lat = location.getLatitude();
			double lng = location.getLongitude();
			return transformToSpark(lat, lng);
		}
		return geoPoint;
	}

	public static GeoPoint getPoint(double lat, double lng) {
		return transformToSpark(lat, lng);
	}

	private static GeoPoint transformToSpark(double lat, double lng) {
		return new GeoPoint((int) ((lat * 1E6)) + DEVIANTLAT,
				(int) ((lng * 1E6)) + DEVIANTLNG);
	}

	public static boolean isAvailableGoogleMap() {
		try {
			Class.forName("com.google.android.maps.MapView");
			Class.forName("com.google.android.maps.GeoPoint");
			Class.forName("com.google.android.maps.MapController");
			Class.forName("com.google.android.maps.Overlay");
			Class.forName("com.google.android.maps.OverlayItem");
			Class.forName("com.google.android.maps.Projection");
			Class.forName("com.google.android.maps.MapActivity");
			return true;
		} catch (Exception localException) {
			return false;
		}
	}

	public static void openGpsDialog(final Context context, int msgId,
			final AbstractCallBack finshMethod) {
		ActivityUtil.showConfrimDialog(context, R.string.text_location_no,
				R.string.text_location_yes, R.string.text_location_title,
				msgId, new ConfirmDialogListener() {
					@Override
					public void onPositive(DialogInterface dialog) {
						Intent intent = new Intent(
								Settings.ACTION_LOCATION_SOURCE_SETTINGS);
						((Activity) context).startActivityForResult(intent, 1);
					}

					@Override
					public void onNegative(DialogInterface dialog) {
						dialog.cancel();
						finshMethod.doCallBack();
					}
				});

	}

}
